package cn.edu.nuc.fourth.services.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.nuc.fourth.dao.AnnounceMapper;
import cn.edu.nuc.fourth.dao.AprojectMapper;
import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Announce;
import cn.edu.nuc.fourth.model.Aproject;
import cn.edu.nuc.fourth.services.interfaces.AdminService;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AnnounceMapper mapper;
	@Autowired
	private AprojectMapper amapper;

	@Override
	public PageInfo announceList(Integer current) {

		if (current <= 0) {
			current = 1;
		}
		Integer count = mapper.count();
		PageInfo pageInfo = new PageInfo(current);
		List<Announce> list = mapper.page(pageInfo.getStart(), pageInfo.getOffset());
		pageInfo.setList(list);
		pageInfo.setCount(count);

		return pageInfo;
	}

	/**
	 * 添加公告
	 */
	@Override
	public Integer announceadd(Announce announce) {

		announce.setDepartment("市教育局");
		announce.setAstate(0);
		Integer result = mapper.insert(announce);
		return result;
	}

	/**
	 * 初始化一条公告记录
	 */
	@Override
	public Announce oneAnno(Integer aid) {

		Announce a = mapper.selectByPrimaryKey(aid);
		return a;
	}

	/**
	 * 执行修改
	 */
	@Override
	public Integer announceedit(Announce announce) {

		Integer result = mapper.updateByPrimaryKey(announce);

		return result;
	}

	@Override
	public Integer stateeidt(Announce announce) {

		Integer result = mapper.updateByPrimaryKeySelective(announce);
		return result;
	}

	@Override
	public List<Announce> ajaxlist(Integer astate) {

		List<Announce> list = mapper.findBystate(astate);
		return list;
	}

	@Override
	public Integer upload(MultipartFile main, MultipartFile province, MultipartFile city, MultipartFile onther,
			String path) {

		System.out.println("main:" + main.getOriginalFilename());
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		path += year;
		System.out.println(path);
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}

		Aproject ap = new Aproject();
		ap.setmProject(path);
		ap.setMname(main.getOriginalFilename());
		ap.setpProject(path);
		ap.setPname(province.getOriginalFilename());
		ap.setcProject(path);
		ap.setCname(city.getOriginalFilename());
		ap.setoProject(path);
		ap.setOname(onther.getOriginalFilename());
		ap.setUserid(-1);
		ap.setDate(String.valueOf(year));

		Aproject aproject = amapper.findBydate(String.valueOf(year));
		Integer result;

		if (!"".equals(main.getOriginalFilename())) {

		}
		if (aproject != null) {

			ap.setAprojectid(aproject.getAprojectid());
			if (!("".equals(main.getOriginalFilename())) && main.getOriginalFilename() != null) {
				ap.setmProject(path);
				ap.setMname(main.getOriginalFilename());
				try {

					FileUtils.copyInputStreamToFile(main.getInputStream(), new File(file, main.getOriginalFilename()));
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				ap.setmProject(aproject.getmProject());
				ap.setMname(aproject.getMname());
			}
			if (!"".equals(province.getOriginalFilename()) && province.getOriginalFilename() != null) {
				ap.setpProject(path);
				ap.setPname(province.getOriginalFilename());
				try {
					FileUtils.copyInputStreamToFile(province.getInputStream(),
							new File(file, province.getOriginalFilename()));
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {

				ap.setpProject(aproject.getpProject());
				ap.setPname(aproject.getPname());
			}
			if (!"".equals(city.getOriginalFilename()) && city.getOriginalFilename() != null) {

				ap.setcProject(path);
				ap.setCname(city.getOriginalFilename());

				try {
					FileUtils.copyInputStreamToFile(city.getInputStream(), new File(file, city.getOriginalFilename()));
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				ap.setcProject(aproject.getcProject());
				ap.setCname(aproject.getCname());
			}
			if (!"".equals(onther.getOriginalFilename()) && onther.getOriginalFilename() != null) {

				ap.setoProject(path);
				ap.setOname(onther.getOriginalFilename());
				try {
					FileUtils.copyInputStreamToFile(onther.getInputStream(),
							new File(file, onther.getOriginalFilename()));
				} catch (IOException e) {
					e.printStackTrace();
				}

			} else {

				ap.setoProject(aproject.getoProject());
				ap.setOname(aproject.getOname());
			}
			result = amapper.updateByPrimaryKeySelective(ap);
		} else {

			try {
				FileUtils.copyInputStreamToFile(main.getInputStream(), new File(file, main.getOriginalFilename()));
				FileUtils.copyInputStreamToFile(province.getInputStream(),
						new File(file, province.getOriginalFilename()));
				FileUtils.copyInputStreamToFile(city.getInputStream(), new File(file, city.getOriginalFilename()));
				FileUtils.copyInputStreamToFile(onther.getInputStream(), new File(file, onther.getOriginalFilename()));
			} catch (IOException e) {
				e.printStackTrace();
			}
			result = amapper.insert(ap);
		}

		return result;
	}

	/**
	 * 分页
	 */
	@Override
	public PageInfo projectlist(Integer current) {

		if (current <= 0) {
			current = 1;
		}
		PageInfo pageInfo = new PageInfo(current);

		List<Aproject> list = amapper.page(pageInfo.getStart(), pageInfo.getOffset());
		pageInfo.setList(list);
		Integer count = amapper.count();
		pageInfo.setCount(count);

		return pageInfo;
	}

	@Override
	public Integer projectdelete(Aproject aproject) {

		Aproject a = amapper.selectByPrimaryKey(aproject.getAprojectid());
		String path = a.getmProject();
		if (aproject.getMname() != null) {

			path += "\\" + aproject.getMname();
			a.setMname(null);
			a.setmProject(null);
		}
		if (aproject.getPname() != null) {

			path += "\\" + aproject.getPname();
			a.setPname(null);
			a.setpProject(null);
		}
		if (aproject.getCname() != null) {
			path += "\\" + aproject.getCname();
			a.setCname(null);
			a.setcProject(null);
		}
		if (aproject.getOname() != null) {
			path += "\\" + aproject.getOname();
			a.setOname(null);
			a.setoProject(null);
		}
		System.out.println(path);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}

		Integer result = amapper.updateByPrimaryKey(a);
		return result;
	}

	@Override
	public PageInfo find(String date) {
		PageInfo p = new PageInfo(1);
		if (date == null || "".equals(date)) {

			Integer count = amapper.count();
			List<Aproject> l = amapper.page(p.getStart(), p.getOffset());
			p.setCount(count);
			p.setList(l);
			p.setCurrent(1);
			return p;
		}
		Aproject ap = amapper.findBydate(date);

		p.setCount(1);
		List<Aproject> list = new ArrayList<>();
		list.add(ap);
		p.setList(list);
		return p;
	}

}
